package noten;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import utils.MyHtmlClient;
import utils.ParserUtil;

public class Notenimport {
	
	private String cookievalue;
	private String url;

	public Notenimport(String cookievalue, String url) {
		this.cookievalue = cookievalue;
		this.url = url;
	}
	
	public ArrayList<Modul> getNoten(){
				
		ArrayList<Modul> noten = new ArrayList<Modul>();

		MyHtmlClient htmlClient = new MyHtmlClient();
		String htmlString = htmlClient.getHtmlString(url, cookievalue, null);
		String regex = "<td class=\"text\"\\s?>(.*?)</td>.*?bgcolor=\"#BBD5FF\"><b> (.*?) </b>";

		for (Matcher m = Pattern.compile(regex).matcher(
				ParserUtil.htmlEncoding(ParserUtil.cleanHtml(htmlString))); m.find();) {

			Modul currentModul = new Modul();
			currentModul.setBezeichnung(m.toMatchResult().group(1));

			try {
				String currentValue;
				currentValue = ParserUtil.cleanMessage(m.toMatchResult().group(2));
				currentValue = currentValue.replaceAll("W1", "");
				currentValue = currentValue.replaceAll("W2", "");
				currentModul.setBewertung(Double.parseDouble(currentValue));
			} catch (Exception e) {
				currentModul.setBewertung(0);
			}

			if (m.toMatchResult().group(2).contains("W1"))
				currentModul.setW1(true);

			if (m.toMatchResult().group(2).contains("W2"))
				currentModul.setW2(true);

			noten.add(currentModul);
		}
	        
		return noten;	
	}	
}
